// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/user/api/SysMenuApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/user/model/SysMenuDto'
import DelClass from '@api/user/model/SysMenuDeleteReq'

const AddClass = ListClass
const EditClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'sysMenuQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.landingPage = CORE.maskNone(item.landingPage)
      item.catalogue = CORE.maskNone(item.catalogue)
      item.oldCatalogue = item.catalogue
      item.iconType = /^el-icon/.test(item.icon) ? 1 : 2
    })
    return arr
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    const catalogue = one.catalogue || 'none'
    const landingPage = one.landingPage || 'none'
    return { ...one, catalogue, landingPage, level: 1 }
  },
  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one)
    await CORE.grpc(TIP, CLIENT, 'sysMenuSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEditToAdd: true,
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one)
    await CORE.grpc(TIP, CLIENT, 'sysMenuUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'sysMenuDelete', DTO)
    return CORE.grpcOK(TIP)
  },
}
